package com.hu.zhcc.shiro.controller.xy;

import com.alibaba.fastjson.JSONObject;
import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.entity.lgx.TMember;
import com.hu.zhcc.shiro.entity.xy.dto.TBodyTypeDTO;
import com.hu.zhcc.shiro.entity.xy.pojo.TAssessQuestion;
import com.hu.zhcc.shiro.entity.xy.vo.TBodyTypeDetailVO;
import com.hu.zhcc.shiro.entity.xy.vo.TBodyTypeListVO;
import com.hu.zhcc.shiro.service.lgx.TmemberService;
import com.hu.zhcc.shiro.service.xy.TbodyTypeService;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.lang.reflect.Member;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
@RequestMapping("/tBodyType")
public class TBodyTypeController {

    @Autowired
    private TbodyTypeService service;
    @Autowired
    private TmemberService tmemberService;


    @GetMapping("")
    public ResponseEntity<Page<TBodyTypeListVO>> getTBodyTypeList(@RequestParam(required = false) String name,
                                                              int offset, int limit) throws ParseException {
        Map<String, Object> paremeters = new HashMap<String, Object>();
        if (!StringUtils.isBlank(name)) {
            paremeters.put("name", name);
        }

        Page<TBodyTypeDTO> result = this.service.listBodyType(paremeters, offset, limit);
        List<TBodyTypeListVO> voList = new ArrayList<TBodyTypeListVO>();
        if(result.getRows()==null){
            return ResponseEntity.ok(new Page<TBodyTypeListVO>(result.getTotal(), voList));
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for(TBodyTypeDTO dto : result.getRows()) {
            TBodyTypeListVO vo = new TBodyTypeListVO();
            BeanUtils.copyProperties(dto, vo);

            vo.setCtime(vo.getCreatetime());

            voList.add(vo);
        }
        return ResponseEntity.ok(new Page<TBodyTypeListVO>(result.getTotal(), voList));
    }


    @PostMapping("")
    public ResponseEntity<TBodyTypeDetailVO> saveBodyType(@RequestBody TBodyTypeDetailVO vo) {
        TBodyTypeDTO dto = new TBodyTypeDTO();
        BeanUtils.copyProperties(vo, dto);
        this.service.insertSelective(dto);
        vo.setId(dto.getId());
        return ResponseEntity.status(HttpStatus.CREATED).body(vo);
    }

    @GetMapping("/{id}")
    public ResponseEntity<TBodyTypeDetailVO> getById(@PathVariable("id") int id) {
        TBodyTypeDTO dto = this.service.selectByPrimaryKey(id);
        if(dto == null) {
            return ResponseEntity.notFound().build();
        }

        TBodyTypeDetailVO vo = new TBodyTypeDetailVO();
        BeanUtils.copyProperties(dto, vo);
        return ResponseEntity.ok(vo);
    }

    @PutMapping("/updateTBodyType/{id}")
    public Integer updateTBodyType(@PathVariable("id") int id, @RequestBody String answers) {
//        System.out.println(id);
        TAssessQuestion[][] objects = JSONObject.parseObject(answers, TAssessQuestion[][].class);
        return service.updateTBodyType(id,objects);
    }


 /*   @PutMapping("/{id}")
    public ResponseEntity<Integer> updateTBodyType(@PathVariable("id") int id, @RequestBody TBodyTypeDetailVO vo) {
        TBodyTypeDTO dto = new TBodyTypeDTO();
        BeanUtils.copyProperties(vo, dto);
        int rows = this.service.updateByPrimaryKeySelective(dto);
        return rows == 0 ? ResponseEntity.notFound().build() :
                ResponseEntity.status(HttpStatus.CREATED).body(rows);
    }*/

    @GetMapping("/all")
    public ResponseEntity<List<TBodyTypeListVO>> listAllTBodyType() {
        List<TBodyTypeDTO> dtoList = this.service.listAllTBodyType();
        if(dtoList.size() == 0) {
            return ResponseEntity.notFound().build();
        }
        List<TBodyTypeListVO> voList = new ArrayList<TBodyTypeListVO>(dtoList.size());
        for(TBodyTypeDTO dto : dtoList) {
            TBodyTypeListVO vo = new TBodyTypeListVO();
            BeanUtils.copyProperties(dto, vo);
            voList.add(vo);
        }
        return ResponseEntity.ok(voList);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> removeTBodyType(@PathVariable("id") Integer id) {
//        System.out.println(id);
        return this.service.deleteByPrimaryKey(id) > 0 ?
                ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
    }
    @PostMapping("/addTBodyType/{idCard}")
    public Integer addTBodyType(@PathVariable("idCard") String idCard, @RequestBody String answers){
        TAssessQuestion[][] objects = JSONObject.parseObject(answers, TAssessQuestion[][].class);
        return service.addTBodyType(idCard,objects);
    }

//    t_member/会员表
//    属性名	数据类型	名字	备注
//    id	 int(11)	主键
//    fileNumber	varchar(32)	档案号
//    name	varchar(32)	会员名
//    age	int(11)	年龄
//    sex	varchar(8)	性别
//    idCard	varchar(18)	身份证号码
//    phoneNumber	varchar(11)	手机号码
//    regTime	date	注册时间
//    password	varchar(32)	密码
//    email	varchar(32)	邮箱
//    birthday	date	出生日期
//    remark	varchar(128)	备注
//    createtime	datetime 	创建时间
//    updatetime	datetime 	修改时间

    @GetMapping("getMemberById/{memberId}")
    public ResponseEntity<TMember> getMemberById(@PathVariable("memberId") Integer memberId){
        System.out.println("memberId="+memberId);
        TMember tMember = tmemberService.selectByPrimaryKey(memberId);
        System.out.println(tMember.toString());

        return ResponseEntity.ok(tMember);
    }

    @GetMapping("getMemberByCardId")
    public ResponseEntity<TMember> getMemberByCardId(@RequestBody String cardId){

        TMember tMember = tmemberService.queryByIdCard(cardId);

        if (tMember == null){
            return ResponseEntity.ok(null);
        }

        return ResponseEntity.ok(tMember);
    }


}
